package cn.com.byte1024.service.impl;

import cn.com.byte1024.entity.Vote;
import cn.com.byte1024.entity.VoteLog;
import cn.com.byte1024.entity.VoteProject;
import cn.com.byte1024.entity.VoteProjectVoteKey;
import cn.com.byte1024.mapper.VoteLogMapper;
import cn.com.byte1024.mapper.VoteMapper;
import cn.com.byte1024.mapper.VoteProjectMapper;
import cn.com.byte1024.mapper.VoteProjectVoteMapper;
import cn.com.byte1024.service.VoteService;
import cn.com.byte1024.utils.CommonUtil;
import java.text.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: VoteSystem
 * @ClassName: VoteServiceImpl
 * @Description:
 * @Author: Kwnda
 * @Date: 2021/1/6 2:40
 */
@Service
public class VoteServiceImpl implements VoteService {
    
    @Autowired
    private VoteMapper voteMapper;

    @Autowired
    private VoteProjectVoteMapper voteProjectVoteMapper;

    @Autowired
    private VoteProjectMapper voteProjectMapper;

    @Autowired
    private VoteLogMapper voteLogMapper;

    @Override
    public List<Vote> selectAllVoteByVoteProjectId(String voteProjectId) {
        List<VoteProjectVoteKey> voteProjectVoteList=voteProjectVoteMapper.selectAllVoteProjectVoteByVoteProjectId(voteProjectId);
        List<Vote> voteList = new ArrayList<>();
        for (VoteProjectVoteKey voteProjectVoteKey:voteProjectVoteList) {
           Vote vote =voteMapper.selectByPrimaryKey(voteProjectVoteKey.getVoteId());
           voteList.add(vote);
        }
        return voteList;
    }

    @Override
    public int insertSelective(Vote vote,String voteProjectId) {
        VoteProjectVoteKey voteProjectVoteKey = new VoteProjectVoteKey();
        voteProjectVoteKey.setVoteId(vote.getVoteId());
        voteProjectVoteKey.setVoteProjectId(voteProjectId);
        voteProjectVoteMapper.insertSelective(voteProjectVoteKey);
        return voteMapper.insertSelective(vote);
    }

    @Override
    public boolean deleteVote(String voteProjectId, String voteId, String userId) {
        boolean flag;
        //根据请求userId判断和发起者的id比较
        VoteProject voteProject =voteProjectMapper.selectByVoteProjectIdUserId(voteProjectId,userId);
        if(voteProject!=null){
            //删除表vote
            voteMapper.deleteByPrimaryKey(voteId);
            //删除表vote_project_vote
            voteProjectVoteMapper.deleteByVoteId(voteId);
            //删除表vote_log
            voteLogMapper.deleteByVoteId(voteId);
            flag=true;
        }else{
            flag=false;
        }
        return flag;
    }

    @Override
    public Vote selectByPrimaryKey(String voteId) {
        return voteMapper.selectByPrimaryKey(voteId);
    }

    @Override
    public int updateByPrimaryKeySelective(Vote record) {
        return voteMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public String voteAdd(String voteProjectId, String voteId, String userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //通过项目id查询项目
        VoteProject voteProject = voteProjectMapper.selectByPrimaryKey(voteProjectId);
        try {
            if(!CommonUtil.hourMinuteBetween(sdf.format(new Date()),voteProject.getStartTime(),voteProject.getEndTime())){
                return "不在投票时间内，请在"+voteProject.getStartTime()+"至"+voteProject.getEndTime()+"时间内进行投票";
            }
        }catch (Exception e){
            
        }

        //通过项目id查询该项目下所有的投票
        List<VoteProjectVoteKey> voteProjectVoteKeyList = voteProjectVoteMapper.selectAllVoteProjectVoteByVoteProjectId(voteProjectId);
        int userVoteProjectNum=0;//该投票项目用户已经投了多少票
        //统计该用户在项目投的总票数
        for (VoteProjectVoteKey voteProjectVoteKey:voteProjectVoteKeyList) {
            List<VoteLog> allVoteLogList = voteLogMapper.selectByVoteIdAndUserId(voteProjectVoteKey.getVoteId(),userId);
            userVoteProjectNum += allVoteLogList.size();
        }
        
        List<VoteLog> voteLogList=voteLogMapper.selectByUserId(userId);
        int userVoteNum=0;//该投票用户已经投了多少票
        //统计该用户在投票投的总票数
        for (VoteLog voteLog:voteLogList) {
           if(voteId.equals(voteLog.getVoteId())){
               userVoteNum++;
           }
        }
        //用户在该项目所投的票数是否大于规定的投票项目的票数，有剩余则继续
        if(userVoteProjectNum < voteProject.getProjectVoteTotal()){
            //用户在该投票所投的票数是否大于规定的投票的票数，有剩余则继续
            if(userVoteNum < voteProject.getSingleVote()){
                //都有剩余则将投票加1
                Vote vote = voteMapper.selectByPrimaryKey(voteId);
                vote.setVoteTicket(vote.getVoteTicket()+1);
                //向数据库更新投票
                voteMapper.updateByPrimaryKeySelective(vote);
                //添加投票记录
                VoteLog voteLog = new VoteLog();
                voteLog.setVoteLogId(CommonUtil.generateUUID());
                voteLog.setVoteId(voteId);
                voteLog.setUserId(userId);
                voteLog.setVoteLogTime(sdf.format(new Date()));
                voteLogMapper.insertSelective(voteLog);
            }else{
                //无次数则提醒
                return "您在该投票的次数已经用完";
            }
        }else{
            //无次数则提醒
            return "您在该项目的投票次数已经用完";
        }

        return "success";
    }
}
